package com.ruoyi.project.purchase.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.purchase.domain.RyDepotHead;
import com.ruoyi.project.purchase.domain.pojo.OrderBillBean;
import com.ruoyi.project.purchase.mapper.RyDepotHeadMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.purchase.mapper.RyOrderBillMapper;
import com.ruoyi.project.purchase.domain.RyOrderBill;
import com.ruoyi.project.purchase.service.IRyOrderBillService;

/**
 * 订单关联发票信息Service业务层处理
 *
 * @author 萧雪
 * @date 2022-10-25
 */
@Service
public class RyOrderBillServiceImpl implements IRyOrderBillService
{
    @Autowired
    private RyOrderBillMapper ryOrderBillMapper;

    @Autowired
    private RyDepotHeadMapper ryDepotHeadMapper;


    /**
     * 查询订单关联发票信息
     *
     * @param id 订单关联发票信息主键
     * @return 订单关联发票信息
     */
    @Override
    public RyOrderBill selectRyOrderBillById(Long id)
    {
        return ryOrderBillMapper.selectRyOrderBillById(id);
    }

    /**
     * 根据订单id 查询发票信息
     *
     * @param orderId
     * @return
     */
    @Override
    public List<RyOrderBill> selectRyOrderBillByOrderId(Long orderId){
        return ryOrderBillMapper.selectRyOrderBillByOrderId(orderId);
    }
    /**
     * 查询订单关联发票信息列表
     *
     * @param ryOrderBill 订单关联发票信息
     * @return 订单关联发票信息
     */
    @Override
    public List<RyOrderBill> selectRyOrderBillList(RyOrderBill ryOrderBill)
    {
        return ryOrderBillMapper.selectRyOrderBillList(ryOrderBill);
    }

    /**
     * 新增订单关联发票信息
     *
     * @param ryOrderBill 订单关联发票信息
     * @return 结果
     */
    @Override
    public int insertRyOrderBill(RyOrderBill ryOrderBill)
    {
        ryOrderBill.setCreateTime(DateUtils.getNowDate());
        return ryOrderBillMapper.insertRyOrderBill(ryOrderBill);
    }

    /**
     * 新增订单关联发票信息
     */
    @Override
    public AjaxResult addOrderBill(OrderBillBean orderBillBean){
        //处理数据
        if(orderBillBean.getId() == null){
            return AjaxResult.error("订单号不能为空");
        }

        if(orderBillBean.getBillTableData() == null || orderBillBean.getBillTableData().size() == 0){
            return AjaxResult.error("发票信息不能为空");
        }

        //如果发票附件不为空则需要保存到表中，以修改为准
        if(StringUtils.isNotBlank(orderBillBean.getBillFileName())){
            //根据订单id修改bill_file_name字段
            RyDepotHead ryDepotHead = new RyDepotHead();
            ryDepotHead.setId(orderBillBean.getId());
            ryDepotHead.setBillFileName(orderBillBean.getBillFileName());
            ryDepotHeadMapper.updateRyDepotHead(ryDepotHead);
        }

        //首先根据订单id删除发票信息（新增和修改都适用）
        ryOrderBillMapper.deleteRyOrderBillByOrderId(orderBillBean.getId());

        //遍历发票信息插入数据库
        for(RyOrderBill orderBill : orderBillBean.getBillTableData()){
            //发票明细绑定订单id
            orderBill.setOrderId(orderBillBean.getId());
            //插入
            ryOrderBillMapper.insertRyOrderBill(orderBill);
        }

        return AjaxResult.success("关联成功");
    }

    /**
     * 修改订单关联发票信息
     *
     * @param ryOrderBill 订单关联发票信息
     * @return 结果
     */
    @Override
    public int updateRyOrderBill(RyOrderBill ryOrderBill)
    {
        ryOrderBill.setUpdateTime(DateUtils.getNowDate());
        return ryOrderBillMapper.updateRyOrderBill(ryOrderBill);
    }

    /**
     * 批量删除订单关联发票信息
     *
     * @param ids 需要删除的订单关联发票信息主键
     * @return 结果
     */
    @Override
    public int deleteRyOrderBillByIds(Long[] ids)
    {
        return ryOrderBillMapper.deleteRyOrderBillByIds(ids);
    }

    /**
     * 删除订单关联发票信息信息
     *
     * @param id 订单关联发票信息主键
     * @return 结果
     */
    @Override
    public int deleteRyOrderBillById(Long id)
    {
        return ryOrderBillMapper.deleteRyOrderBillById(id);
    }
}
